package com.example.huoher.miniaddressbook;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

public class ContactsActivity extends AppCompatActivity {

    private Context context;
    private SQLiteDatabase db;
    private MyDBOpenHelper myDBHelper;
    private StringBuilder sb;
    private int pid = 0;
//    private int i;
//
//    private Button button1;
//    private Button button2;

    private List<Person> mData= null;
    private PersonAdapter personAdapter = null;
    private ListView listView;

    private Intent intent1;
    private Intent intent2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);

        context = ContactsActivity.this;
        myDBHelper = new MyDBOpenHelper(context,"my.db",null,1);
        db = myDBHelper.getWritableDatabase();

        listView = findViewById(R.id.list_person);
        mData = new LinkedList<Person>();
        mData.add(new Person("zd", "11111"));
        mData.add(new Person("你是牛么?","2222"));
//        mData.add(new Person( "你是鸭么?", "2131"));
//        mData.add(new Person( "你是鱼么?","13213"));
//        mData.add(new Person( "你是马么?","232131"));
//        sb = new StringBuilder();
//        ContentValues contentValues = new ContentValues();
//        contentValues.put("name","hehe~");
//        contentValues.put("number","12321312");
//        db.insert("person",null,contentValues);

        Cursor cursor = db.query("person", null, null, null, null, null, null);
        if (cursor.moveToFirst()){
            do {
//                int pid = cursor.getInt(cursor.getColumnIndex("personid"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String number = cursor.getString(cursor.getColumnIndex("number"));
////                sb.append("id：" + pid + "：" + name + "\n");
////                pid ++;
                mData.add(new Person(name,number));
            }while (cursor.moveToNext());
        }
        cursor.close();
        personAdapter = new PersonAdapter((LinkedList<Person>) mData,context);
        listView.setAdapter(personAdapter);
        intent1 = new Intent(ContactsActivity.this,AddpeopleActivity.class);
        intent2 = new Intent(ContactsActivity.this,MainActivity.class);

//
//        bindViews();
    }

//    private void bindViews(){
//        button1 = findViewById(R.id.button_add);
//        button2 = findViewById(R.id.button_show);
//
//        button1.setOnClickListener(this);
//        button2.setOnClickListener(this);
//    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.start:
                startActivity(intent1);
                ContactsActivity.this.finish();
                break;
//                Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
            case R.id.over:
                 startActivity(intent2);
                ContactsActivity.this.finish();
                break;
//                Toast.makeText(this, "ddd", Toast.LENGTH_SHORT).show();
            case R.id.query:
                Toast.makeText(this, "您已经在这个界面!", Toast.LENGTH_SHORT).show();
//                ContactsActivity.this.finish();
                break;
//            default:
//                break;
        }
        return super.onOptionsItemSelected(item);
    }

//    @Override
//    public void onClick(View view) {
//        db = myDBHelper.getWritableDatabase();
//        switch (view.getId()){
//            case R.id.button_add:
//                ContentValues contentValues = new ContentValues();
//                contentValues.put("name","hehe~"+i);
//                i++;
//                db.insert("person",null,contentValues);
//                Toast.makeText(context, "插入完毕", Toast.LENGTH_SHORT).show();
//            case R.id.button_show:
//                sb = new StringBuilder();
//                Cursor cursor = db.query("person", null, null, null, null, null, null);
//                if (cursor.moveToFirst()){
//                    do {
//                        int pid = cursor.getInt(cursor.getColumnIndex("personid"));
//                        String name = cursor.getString(cursor.getColumnIndex("name"));
//                        sb.append("id：" + pid + "：" + name + "\n");
//                    }while (cursor.moveToNext());
//                }
//
//                cursor.close();
//                Toast.makeText(context, sb.toString(), Toast.LENGTH_SHORT).show();
//            default:
//                break;
//        }
//    }
}
